package de.mmis.core.base.abstracttree.serializer.internal;

import java.util.Map;

import de.mmis.core.base.DeserializableAnnotations.DeserializationMethod;
import de.mmis.core.base.abstracttree.InnerNode;
import de.mmis.core.base.abstracttree.Tree;
import de.mmis.core.base.abstracttree.serializer.AbstractSerializer;
import de.mmis.core.base.abstracttree.serializer.selector.ListSelector;

/**
 * Serializer class for (de)serializing {@link Iterable} object from/to Trees
 * 
 * @author Christian Koch
 */
public class IterableSerializer extends AbstractSerializer<Iterable<?>> {

	@DeserializationMethod
	public static Object deserialize(Tree[] exps, ClassLoader classLoader,
			String id, Map<String, Object> idmap) {

		return new ListSelector(new InnerNode(exps), classLoader, id, idmap);
	}

	@Override
	public Tree serializeInternal(Iterable<?> object, boolean explicit,
			Map<Object, String> refmap, ClassLoader classLoader) {
		InnerNode ret = new InnerNode();
		for (Object elem : object)
			ret.add(Tree.fromObject(elem, explicit, refmap, classLoader));
		return ret;
	}

	@Override
	public String getTypeIdentifier() {
		return "list";
	}

}
